﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace Story.Library
{
    /// <summary>
    /// 读取INI配置文件
    /// </summary>
   public class IniHelper
    {
       public string path = string.Empty;
       [DllImport("kernel32")]
       private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
       [DllImport("kernel32")]
       private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);        //声明读写INI文件的API函数
       public IniHelper(string INIPath)
       {
           path = INIPath;
           if (!File.Exists(path))
           {
               File.Create(path);
           }
       }
       /// <summary>
       /// 写入配置
       /// </summary>
       /// <param name="Section">字段名</param>
       /// <param name="Key">键</param>
       /// <param name="Value">值</param>
       public void write(string Section, string Key, string Value)
       {
           WritePrivateProfileString(Section, Key, Value, this.path);
       }
       /// <summary>
       /// 读取配置
       /// </summary>
       /// <param name="Section">字段名</param>
       /// <param name="Key">键</param>
       /// <returns></returns>
       public string read(string Section, string Key)
       {
           StringBuilder temp = new StringBuilder(255);
           int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
           return temp.ToString();
       }
    }
}
